﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using DynamicData;
using MusicFluent.Models;
using MusicFluent.ViewModels;
using MusicFluent.Views;
using NetEasyMusic.Lib.Data;
using NetEasyMusic.Lib.Models;
using NetEasyMusic.Lib.Operations;
using AlbumView = MusicFluent.MusicViews.AlbumView;
using ArtistView = MusicFluent.MusicViews.ArtistView;

namespace MusicFluent.Controls;

public partial class SongControl : UserControl
{
    private SongModel _model;

    public SongControl(SongModel model, bool isAlbum = false)
    {
        _model = model;
        InitializeComponent();
        NameBlock.Text = model.Name;
        if(!string.IsNullOrEmpty(model.Time))
            TimeBlock.Text = int.Parse(model.Time).ToTimeString();
        var authors = new List<ArtistModel>();
        if (model.Artists.Length > 2)
        {
            authors.Add(model.Artists[0]);
            authors.Add(model.Artists[1]);
        }
        else
        {
            authors.Add(model.Artists);
        }

        AuthorsList.ItemsSource = authors;
        AlbumButton.Content = model.Album;
        AlbumButton.IsVisible = !isAlbum;
    }

    protected override void OnInitialized()
    {
        if (!AlbumButton.IsVisible) return;
        Image.Source = $"{_model.PicUrl}?param=40y40";
    }

    private void ArtistTapped(object? sender, TappedEventArgs e)
    {
        if (sender is not Control control) return;
        if (control.DataContext is not ArtistModel model) return;
        var view = ViewOpera.GetView<MainWindow>(Parent as Control);
        if (view == null) return;
        var artist = new ArtistView(model, view.Cookie);
        view.Navigate(artist);
    }

    private async void MusicDoubleTapped(object? sender, TappedEventArgs e)
    {
        await Play();
    }

    private async Task Play()
    {
        var view = ViewOpera.GetView<MainWindow>(Parent);
        if (view == null) return;
        if (string.IsNullOrEmpty(_model.Time))
        {
            using var music = new Music(view.Cookie);
            _model = await music.GetSongFormId(_model.Id);
        }
        view.SongPanel.DataContext = _model;
        if (view.DataContext is not MainWindowViewModel model) return;
        model.Add(ViewOpera.GetList(this));
        model.PlayChange(_model.Id);
    }

    private void AlbumClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(_model.Album)) return;
        var view = ViewOpera.GetView<MainWindow>(Parent);
        if (view == null) return;
        var artist = new AlbumView(_model.AlbumId);
        view.Navigate(artist);
    }

    private void AddItemClick(object? sender, RoutedEventArgs e)
    {
        var model = ViewOpera.GetViewData<MainWindowViewModel>(Parent);
        model?.Add(_model);
    }

    private async void PlayClick(object? sender, RoutedEventArgs e)
    {
        await Play();
    }

    private void DetailClick(object? sender, RoutedEventArgs e)
    {
        
    }
}